package main

import (
	chessgame "chess/config"
	log2 "chess/log"
	"chess/service"
	"context"
	"flag"
	"fmt"
	"google.golang.org/grpc"
	"log"
	"net"
)

var ChessGameAddr = flag.Int("ChessGameAddr", 16014, "the address to connect to")

func main() {
	flag.Parse()
	lis, err := net.Listen("tcp", fmt.Sprintf("chessgame:%d", *ChessGameAddr))
	if err != nil {
		writeLog, err2 := log2.WriteLog(context.Background(), "chessgame", "GRPC server", "chessgame GRPC server", "Error", err.Error())
		if writeLog == 200 {
			fmt.Println("成功写入日志")
		}
		if err2 != nil {
			fmt.Println("写入日志失败")
		}
		log.Fatalf("failed to listen: %v", err)
	}
	s := grpc.NewServer()
	//leaderboard.RegisterProdScoreBoardServiceServer(s, &service.Rank{})
	chessgame.RegisterChessGameServer(s, &service.ChessGame{})
	log.Printf("server listening at %v", lis.Addr())
	if err := s.Serve(lis); err != nil {
		writeLog, err2 := log2.WriteLog(context.Background(), "chessgame", "GRPC server", "chessgame GRPC server", "Error", err.Error())
		if writeLog == 200 {
			fmt.Println("成功写入日志")
		}
		if err2 != nil {
			fmt.Println("写入日志失败")
		}
		log.Fatalf("failed to serve: %v", err)
	}

}
